// 歌词解析工具
class LyricParser {
  static parseLyric(lrcText) {
    if (!lrcText) return [];
    
    const lines = lrcText.split('\n');
    const lyricData = [];
    
    for (let line of lines) {
      const result = this.parseLine(line);
      if (result) {
        lyricData.push(result);
      }
    }
    
    // 按时间排序
    lyricData.sort((a, b) => a.time - b.time);
    
    return lyricData;
  }

  static parseLine(line) {
    // 匹配时间标签 [00:00.00]
    const timeRegex = /\[(\d+):(\d+)\.(\d+)\]/g;
    const text = line.replace(timeRegex, '').trim();
    
    if (!text) return null;
    
    const times = [];
    let match;
    
    while ((match = timeRegex.exec(line)) !== null) {
      const minutes = parseInt(match[1]);
      const seconds = parseInt(match[2]);
      const milliseconds = parseInt(match[3]);
      const time = minutes * 60 + seconds + milliseconds / 100;
      times.push(time);
    }
    
    if (times.length === 0) return null;
    
    return {
      time: times[0], // 取第一个时间标签
      text: text
    };
  }

  // 生成模拟歌词数据
  static generateMockLyric(songName, duration) {
    const baseLyrics = {
      "夜曲": [
        { time: 0, text: "夜曲 - 周杰伦" },
        { time: 10, text: "一群嗜血的蚂蚁 被腐肉所吸引" },
        { time: 15, text: "我面无表情 看孤独的风景" },
        { time: 25, text: "失去你 爱恨开始分明" },
        { time: 35, text: "失去你 还有什么事好关心" },
        { time: 50, text: "当鸽子不再象征和平" },
        { time: 60, text: "我终于被提醒 广场上喂食的是秃鹰" }
      ],
      "青花瓷": [
        { time: 0, text: "青花瓷 - 周杰伦" },
        { time: 10, text: "素胚勾勒出青花笔锋浓转淡" },
        { time: 20, text: "瓶身描绘的牡丹一如你初妆" },
        { time: 30, text: "冉冉檀香透过窗心事我了然" },
        { time: 40, text: "宣纸上走笔至此搁一半" }
      ],
      "夜的钢琴曲": [
        { time: 0, text: "夜的钢琴曲 - 石进" },
        { time: 30, text: "钢琴声在夜色中流淌" },
        { time: 60, text: "如月光般温柔宁静" },
        { time: 90, text: "每一个音符都是夜的诉说" }
      ]
    };
    
    return baseLyrics[songName] || [
      { time: 0, text: `${songName} - 纯音乐` },
      { time: duration / 2, text: "享受美妙的音乐时光" }
    ];
  }
}

module.exports = LyricParser;